package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GysptMaterialType;
import com.ruoyi.gcgl.service.IGysptMaterialTypeService;

/**
 * 材料分类Controller
 * 
 * @author ruoyi
 * @date 2024-10-02
 */
@Controller
@RequestMapping("/gcgl/type")
public class GysptMaterialTypeController extends BaseController
{
    private String prefix = "gcgl/type";

    @Autowired
    private IGysptMaterialTypeService gysptMaterialTypeService;

    @RequiresPermissions("gcgl:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询材料分类列表
     */
    @RequiresPermissions("gcgl:type:list")
    @PostMapping("/list")
    @ResponseBody
    public List<GysptMaterialType> list(GysptMaterialType gysptMaterialType)
    {
    	gysptMaterialType.setDeleteFlag(1);
        List<GysptMaterialType> list = gysptMaterialTypeService.selectGysptMaterialTypeList(gysptMaterialType);
        return list;
    }

    /**
     * 导出材料分类列表
     */
    @RequiresPermissions("gcgl:type:export")
    @Log(title = "材料分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GysptMaterialType gysptMaterialType)
    {
        List<GysptMaterialType> list = gysptMaterialTypeService.selectGysptMaterialTypeList(gysptMaterialType);
        ExcelUtil<GysptMaterialType> util = new ExcelUtil<GysptMaterialType>(GysptMaterialType.class);
        return util.exportExcel(list, "材料分类数据");
    }

    /**
     * 新增材料分类
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
    	GysptMaterialType gysptMaterialType = new GysptMaterialType();
    	if (0L != parentId){
    		gysptMaterialType = gysptMaterialTypeService.selectGysptMaterialTypeById(parentId);
        }else{
        	gysptMaterialType = new GysptMaterialType();
        	gysptMaterialType.setId(0l);
        	gysptMaterialType.setName("主目录");
        }
    	
        mmap.put("gysptMaterialType", gysptMaterialType);
        return prefix + "/add";
    }

    /**
     * 新增保存材料分类
     */
    @RequiresPermissions("gcgl:type:add")
    @Log(title = "材料分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GysptMaterialType gysptMaterialType)
    {
    	gysptMaterialType.setDeleteFlag(1);
    	gysptMaterialType.setCreateAt(new Date());
    	gysptMaterialType.setCreateUser(getLoginName());
    	gysptMaterialType.setUpdateAt(new Date());
    	gysptMaterialType.setUpdateUser(getLoginName());
        return toAjax(gysptMaterialTypeService.insertGysptMaterialType(gysptMaterialType));
    }

    /**
     * 修改材料分类
     */
    @RequiresPermissions("gcgl:type:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GysptMaterialType gysptMaterialType = gysptMaterialTypeService.selectGysptMaterialTypeById(id);
        mmap.put("gysptMaterialType", gysptMaterialType);
        return prefix + "/edit";
    }

    /**
     * 修改保存材料分类
     */
    @RequiresPermissions("gcgl:type:edit")
    @Log(title = "材料分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GysptMaterialType gysptMaterialType)
    {
    	gysptMaterialType.setUpdateAt(new Date());
    	gysptMaterialType.setUpdateUser(getLoginName());
        return toAjax(gysptMaterialTypeService.updateGysptMaterialType(gysptMaterialType));
    }

    /**
     * 删除材料分类
     */
    @RequiresPermissions("gcgl:type:remove")
    @Log(title = "材料分类", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{ids}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("ids") String ids)
    {
        return toAjax(gysptMaterialTypeService.deleteGysptMaterialTypeByIds(ids));
    }
    
    /**
     * 选择部门树
     * 
     * @param deptId 部门ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectTypeTree/{id}" })
    public String selectTypeTree(@PathVariable("id") Long id, ModelMap mmap){
    	GysptMaterialType gysptMaterialType = gysptMaterialTypeService.selectGysptMaterialTypeById(id);
        mmap.put("gysptMaterialType", gysptMaterialType);
        return prefix + "/tree";
    }
    
    /**
     * 加载部门列表树（排除下级）
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(){
    	GysptMaterialType type = new GysptMaterialType();
    	type.setDeleteFlag(1);
        List<Ztree> ztrees = gysptMaterialTypeService.selectTypeTree(type);
        return ztrees;
    }
}